import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义store
export const useUserInfoStore = defineStore(
	'userInfo',
	() => {
		const userInfo = ref({
			accountNumber: "",
			currentStudyWordBankId: "",
			password: "",
			role: 0,
			userName: "",
			uuid: "",
			wxOpenid: "",
			studyNewTimes: 0,
			reviewTimes: 0,
			_id: "",
		})
		
		const selectedStudentInfo = ref({
			userName: "",
			uuid: "",
			studyNewTimes: 0,
			reviewTimes: 0,
		})
		
		const setUserInfo = (val) => {
			userInfo.value = val
		}
		
		const clearUserInfo = () => {
			userInfo.value = {
				accountNumber: "",
				currentStudyWordBankId: "",
				password: "",
				role: 0,
				userName: "",
				uuid: "",
				wxOpenid: "",
				studyNewTimes: 0,
				reviewTimes: 0,
				_id: "",
			}
		}
		
		const setSelectedStudentInfo = (val) => {
			selectedStudentInfo.value = val
		}
		
		const clearSelectedStudentInfo = () => {
			selectedStudentInfo.value = {
				userName: "",
				uuid: "",
				studyNewTimes: 0,
				reviewTimes: 0,
			}
		}
		
		return {
			userInfo,
			setUserInfo,
			clearUserInfo,
			
			selectedStudentInfo,
			setSelectedStudentInfo,
			clearSelectedStudentInfo,
		}
	},
	
	//持久化
	{
		//配置持久化
		persist: {
			storage: {
				setItem(key, value) {
					uni.setStorageSync(key, value)
				},
				getItem(key) {
					return uni.getStorageSync(key)
				},
			}
		}
	}
)